﻿/*******************************************************************************
Copyright (c) 2012-2013, PKStudio

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace PKEngine
{
	/// <summary>
	/// MainWindow of PKEngine.
	/// </summary>
	public partial class PKWindow : Form
	{
		private PKMouseEvent mouseEvent;
		private PKMouseEvent mouseMoveEvent;
		private PKKeybdEvent keybdEvent_Up;
		private PKKeybdEvent keybdEvent_Down;
		private PKKeybdEvent keybdEvent_Left;
		private PKKeybdEvent keybdEvent_Right;
		private PKKeybdEvent keybdEvent_Ok;
		private PKKeybdEvent keybdEvent_Cancel;
		private PKKeybdEvent keybdEvent_Prev;
		private PKKeybdEvent keybdEvent_Next;

		public PKWindow()
		{
			InitializeComponent();

			// Lock the shared config.
			PKConfig.SharedConfig.Lock();

			// Set the window title.
			this.Text = PKConfig.SharedConfig.GameName;

			// Make sure the screen size is correct.
			this.Width += (int)(PKConfig.SharedConfig.ScreenWidth * PKConfig.SharedConfig.ScreenScaleX) - this.MainScreen.Width;
			this.Height += (int)(PKConfig.SharedConfig.ScreenHeight * PKConfig.SharedConfig.ScreenScaleY) - this.MainScreen.Height;

			this.mouseMoveEvent = new PKMouseEvent(0, 0);
		}

		private void MainFormActivated(object sender, EventArgs e)
		{
			// Resume the game
			PKGame.SharedGame.Resume();
			this.mouseMoveEvent = new PKMouseEvent(0, 0);
		}

		private void MainFormDeactivate(object sender, EventArgs e)
		{
			// Pause the game.
			PKGame.SharedGame.Pause();
			this.mouseMoveEvent = null;
		}

		protected virtual void RunStartScene()
		{
			// Override me.
		}

		private void MainFormLoad(object sender, EventArgs e)
		{
			// Start the main loop.
			new Thread(delegate() { PKGame.SharedGame.MainLoop(this.MainScreen); }).Start();

			// Run the game.
			this.RunStartScene();
		}

		private void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				// End the game.
				PKGame.SharedGame.End();

				// Cancel the closing. Or maybe main form will be closed before we exit main loop.
				e.Cancel = true;
			}
		}

		void MainScreenMouseDown(object sender, MouseEventArgs e)
		{
			if (0 != (e.Button & MouseButtons.Left))
			{
				int x = (int)(e.X / PKConfig.SharedConfig.ScreenScaleX);
				if (x < 0)
				{
					x = 0;
				}
				if (x >= PKConfig.SharedConfig.ScreenWidth)
				{
					x = PKConfig.SharedConfig.ScreenWidth - 1;
				}
				int y = (int)(e.Y / PKConfig.SharedConfig.ScreenScaleY);
				if (y < 0)
				{
					y = 0;
				}
				if (y >= PKConfig.SharedConfig.ScreenHeight)
				{
					y = PKConfig.SharedConfig.ScreenHeight - 1;
				}
				mouseEvent = new PKMouseEvent(x, y);
				PKMouseDispatcher.SharedDispatcher.MouseDown(mouseEvent);
			}
		}

		void MainScreenMouseMove(object sender, MouseEventArgs e)
		{
			int x = (int)(e.X / PKConfig.SharedConfig.ScreenScaleX);
			if (x < 0)
			{
				x = 0;
			}
			if (x >= PKConfig.SharedConfig.ScreenWidth)
			{
				x = PKConfig.SharedConfig.ScreenWidth - 1;
			}
			int y = (int)(e.Y / PKConfig.SharedConfig.ScreenScaleY);
			if (y < 0)
			{
				y = 0;
			}
			if (y >= PKConfig.SharedConfig.ScreenHeight)
			{
				y = PKConfig.SharedConfig.ScreenHeight - 1;
			}

			if (mouseMoveEvent != null)
			{
				mouseMoveEvent.SetPoint(x, y);
				PKMouseDispatcher.SharedDispatcher.MouseMove(mouseMoveEvent);
			}

			if (mouseEvent != null && 0 != (e.Button & MouseButtons.Left))
			{
				mouseEvent.SetPoint(x, y);
				PKMouseDispatcher.SharedDispatcher.MouseDrag(mouseEvent);
			}
		}

		void MainScreenMouseUp(object sender, MouseEventArgs e)
		{
			if (mouseEvent != null && 0 != (e.Button & MouseButtons.Left))
			{
				int x = (int)(e.X / PKConfig.SharedConfig.ScreenScaleX);
				if (x < 0)
				{
					x = 0;
				}
				if (x >= PKConfig.SharedConfig.ScreenWidth)
				{
					x = PKConfig.SharedConfig.ScreenWidth - 1;
				}
				int y = (int)(e.Y / PKConfig.SharedConfig.ScreenScaleY);
				if (y < 0)
				{
					y = 0;
				}
				if (y >= PKConfig.SharedConfig.ScreenHeight)
				{
					y = PKConfig.SharedConfig.ScreenHeight - 1;
				}
				mouseEvent.SetPoint(x, y);
				PKMouseDispatcher.SharedDispatcher.MouseUp(mouseEvent);
				mouseEvent = null;
			}
		}

		private void MainFormKeyDown(object sender, KeyEventArgs e)
		{
			PKKeybdEvent keybdEvent = null;
			PKKeybdKey key = PKKeybdEvent.GetKeyEnumFromKeyValue(e.KeyValue);
			switch (key)
			{
				case PKKeybdKey.Up:
					keybdEvent = keybdEvent_Up = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Down:
					keybdEvent = keybdEvent_Down = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Left:
					keybdEvent = keybdEvent_Left = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Right:
					keybdEvent = keybdEvent_Right = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Ok:
					keybdEvent = keybdEvent_Ok = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Cancel:
					keybdEvent = keybdEvent_Cancel = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Prev:
					keybdEvent = keybdEvent_Prev = new PKKeybdEvent(key);
					break;
				case PKKeybdKey.Next:
					keybdEvent = keybdEvent_Next = new PKKeybdEvent(key);
					break;
			}
			if (keybdEvent != null)
			{
				PKKeybdDispatcher.SharedDispatcher.KeyDown(keybdEvent);
			}
		}

		private void MainFormKeyUp(object sender, KeyEventArgs e)
		{
			PKKeybdEvent keybdEvent = null;
			PKKeybdKey key = PKKeybdEvent.GetKeyEnumFromKeyValue(e.KeyValue);
			switch (key)
			{
				case PKKeybdKey.Up:
					keybdEvent = keybdEvent_Up;
					keybdEvent_Up = null;
					break;
				case PKKeybdKey.Down:
					keybdEvent = keybdEvent_Down;
					keybdEvent_Down = null;
					break;
				case PKKeybdKey.Left:
					keybdEvent = keybdEvent_Left;
					keybdEvent_Left = null;
					break;
				case PKKeybdKey.Right:
					keybdEvent = keybdEvent_Right;
					keybdEvent_Right = null;
					break;
				case PKKeybdKey.Ok:
					keybdEvent = keybdEvent_Ok;
					keybdEvent_Ok = null;
					break;
				case PKKeybdKey.Cancel:
					keybdEvent = keybdEvent_Cancel;
					keybdEvent_Cancel = null;
					break;
				case PKKeybdKey.Prev:
					keybdEvent = keybdEvent_Prev;
					keybdEvent_Prev = null;
					break;
				case PKKeybdKey.Next:
					keybdEvent = keybdEvent_Next;
					keybdEvent_Next = null;
					break;
			}
			if (keybdEvent != null)
			{
				PKKeybdDispatcher.SharedDispatcher.KeyUp(keybdEvent);
			}
		}
	}
}